/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class User extends AbstractUser implements Serializable {
    
    private static final long serialVersionUID = 1L;
   
    private String firstName;
    private String lastName;
    private Gender gender;
    private String livingPlace;
    private boolean banned;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Notification> notifications;
    
    @OneToOne(cascade = CascadeType.ALL)
    private Calendar calendar;
    
    public User() {
    	super();
    	this.banned = false;
    	this.notifications = new LinkedList<Notification>();
    	this.calendar = new Calendar(this);
    }

    public Event createEvent() {
            return null;
    }

    public List<Notification> getNotifications() {
            return this.notifications;
    }

    public Calendar getCalendar() {
            return calendar;
    }

    public List<Event> getEvents() {
            return getCalendar().getEvents();
    }

    public String getFirstName() {
            return firstName;
    }

    public String getLastName() {
            return lastName;
    }

    public Gender getGender() {
            return gender;
    }

    public String getLivingPlace() {
            return livingPlace;
    }

    public boolean isBanned() {
            return banned;
    }

	public void setFirstName(String string) {
		this.firstName = string;
	}
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	
	public void setNotifications(List<Notification> notifications) {
		this.notifications = notifications;
	}
	
	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	public void addNotification(Notification notification) {
		this.notifications.add(0, notification);		
	}
	
	public List<Object> remEvent(Event e) {
		List<Object> toRemove = new ArrayList<Object>();
		List<Notification> nr = new ArrayList<Notification>();
		
		for(Notification n : this.notifications) {
			if(n.getEvent().getId() == e.getId()) {
				nr.add(n);
				toRemove.add(n);
			}
		}
		
		for(Notification n : nr) {
			notifications.remove(n);
		}
		
		calendar.deleteEvent(e);
		
		return toRemove;
	}
	
	public void declineEvent(Event e) {		
		calendar.deleteEvent(e);
	}
}
